# -*- coding: cp1252 -*-
#!/usr/bin/env python

from ConectaMysql import *

class GerenciaUsuario(ConectaMysql):    
    def __init__(self):        
        ConectaMysql.__init__(self)


    def setUsuario(self, objectUsuario):
        self.__objectUsuario = objectUsuario
        
        insertUsuario = self.executaQuery(
            "INSERT INTO tb_usuario(" +
            "`USUnome`," +
            "`USUcpf`," +
            "`USUlogin`," +
            "`USUsenha`" +
            ") VALUES (" +
            "'" + str(self.__objectUsuario.get_usuNome())  + "'," +
            "'" + str(self.__objectUsuario.get_usuCpf())  + "'," +
            "'" + str(self.__objectUsuario.get_usuLogin())  + "'," +
            "'" + str(self.__objectUsuario.get_usuSenha()) + "'"  +
            ")")


        if (insertUsuario <> False):
            linhaUsuario = self.executaQuery(
            "SELECT USUid FROM tb_usuario WHERE (`USUcpf` = " +
            "'" + str(self.__objectUsuario.get_usuCpf())  + "'" +
            ")")

              
        for linha in linhaUsuario:
            insertEmSetor = self.executaQuery(
            "INSERT INTO tb_usuariosetor (`US_USUid`, `US_SETid`) VALUES (%d, %d)" %(linha["USUid"], self.__objectUsuario.get_usuSetor())
            )

    def getUsuarioAll(self):
        self.__result = self.executaQuery( 
            "SELECT * FROM tb_usuario "+
            "INNER JOIN tb_usuariosetor ON " +
            "tb_usuario.USUid = tb_usuariosetor.US_USUid " +
            "INNER JOIN tb_setor ON " +
            "tb_setor.SETid = tb_usuariosetor.US_SETid")

        self.__arrayUsuario = []    

        for self.__linha in self.__result:
            self.__arrayUsuario.append(self.__linha)

        return self.__arrayUsuario
            

    def getUsuarioPorSetor(self, setor):
        self.__setor = setor

        self.__result = self.executaQuery( 
            "SELECT * FROM tb_usuario "+
            "INNER JOIN tb_usuariosetor ON " +
            "tb_usuario.USUid = tb_usuariosetor.US_USUid " +
            "INNER JOIN tb_setor ON " +
            "tb_setor.SETid = tb_usuariosetor.US_SETid " +
            "WHERE tb_setor.SETid = '" + str(self.__setor) + "'")

        self.__arrayUsuario = []    

        for self.__linha in self.__result:
            self.__arrayUsuario.append(self.__linha)

        return self.__arrayUsuario

    def getUsuarioParametro(self, parametro):
        self.__parametro = parametro
        self.__result = self.executaQuery(
            "SELECT * FROM tb_usuario "+
            "INNER JOIN tb_usuariosetor ON " +
            "tb_usuario.USUid = tb_usuariosetor.US_USUid " +
            "INNER JOIN tb_setor ON " +
            "tb_setor.SETid = tb_usuariosetor.US_SETid " +
            "WHERE (tb_usuario.USUcpf = '" + str(self.__parametro) + "') or " +
            "(tb_usuario.USUnome like " + "'%" + self.__parametro + "%')")

        
        self.__arrayUsuario = []    

        for self.__linha in self.__result:
            self.__arrayUsuario.append(self.__linha)

        return self.__arrayUsuario


    def alterUsuario(self, parametroCpf, objectNovoUsuario):
        self.__parametroCpf = parametroCpf
        self.__objectNovoUsuario = objectNovoUsuario

        alterUsuario = self.executaQuery(
        "UPDATE tb_usuario SET " +
            "USUnome = '" + str(self.__objectUsuario.get_usuNome()) + "', " +
            "USUcpf = '" + str(self.__objectUsuario.get_usuCpf()) + "', " +
            "USUlogin = '" + str(self.__objectUsuario.get_usuLogin()) + "', " +
            "USUsenha = '" + str(self.__objectUsuario.get_usuSenha()) + "' " +
        "WHERE (USUcpf = '" + str(self.__parametroCpf) + "')")


        if (alterUsuario <> False):
            linhaUsuario = self.executaQuery(
                "SELECT USUid FROM tb_usuario WHERE (`USUcpf` = " +
                "'" + str(self.__parametroCpf) + "')")



        for linha in linhaUsuario:
            alterSetor = self.executaQuery(
                "UPDATE tb_usuariosetor SET " +
                "US_SETid = " + str(self.__objectUsuario.get_usuSetor()) +
                "WHERE (US_USUid = '" + str(linhaUsuario[USUid]) + "')")

        

        
